package com.game.server.system;

import java.util.Collection;
import java.util.HashMap;

import com.game.majiang.config.LogManager;
import jdk.nashorn.internal.objects.annotations.Getter;
import lombok.ToString;

@ToString
public class AbstractPlayerBase 
{
	protected int id;
    @Getter
	public int getID()
	{
		return id;
	}
	public void setID(int id)
	{
		this.id = id;
	}
	
    protected HashMap<Integer, AbstractPlayer> players;

    public AbstractPlayerBase(int id) {
        this.id = id;
        players = new HashMap<Integer, AbstractPlayer>();
    }

    //添加一个player
    public boolean addPlayer(AbstractPlayer player) {
        if (players == null) return false;
        synchronized (players) {
        	AbstractPlayer client2 = players.get(player.getUserId());
            if (client2 == null) {
            	players.put(player.getUserId(), player);
            	
                return true;
            } else {
                return false;
            }
        }
    }

    //移除一个player
    public AbstractPlayer removePlayer(int id) {
        if (players == null) return null;
        synchronized (players) {
        	AbstractPlayer plsyer = players.get(id);
            if (plsyer != null) {
                return players.remove(id);
            }
            return null;
        }
    }

    //获取players
    @SuppressWarnings("unchecked")
    public Collection<AbstractPlayer> getPlayers() {
        synchronized (this) {
            return ((HashMap<Integer, AbstractPlayer>) players.clone()).values();
        }
    }

    //获取player人数
    public int GetPlayerCount() {
        if (players == null) return 0;
        return players.size();
    }

   
    //从id获取player
    public AbstractPlayer getPlayer(int id) {
        synchronized (this) {
            if (players == null) return null;
            for (AbstractPlayer player : players.values()) {
                if (id == player.getUserId()) {
                    return player;
                }
            }
        }
        return null;
    }
}
